package com.song.tools.songtest.config;

import com.song.tools.core.beans.LogId;
import com.song.tools.core.util.ObjectUtils;
import com.song.tools.log.operatelog.service.DefaultOperateLogProcessor;
import com.song.tools.log.operatelog.service.OperateLogProcessor;
import com.song.tools.log.systemlog.core.SysLogVo;
import com.song.tools.log.systemlog.event.SysLogEventListener;
import com.song.tools.mvc.convertor.DesensitizedOnJsonConvertor;
import com.song.tools.songtest.beans.request.SystemLogSaveRequest;
import com.song.tools.songtest.service.SystemLogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * 日志配置类
 *
 * @author song tools
 */
@Slf4j
@Configuration
public class LogConfig {

    @Resource
    private SystemLogService systemLogService;

    @Resource
    private DistributedSnowflake distributedSnowflake;

    @Bean
    @ConditionalOnProperty(name = {"log-tool.log.system.enable"}, havingValue = "true")
    @Primary
    public SysLogEventListener sysLogEventListener() {
        List<Consumer<SysLogVo>> consumers = new ArrayList<>();
        consumers.add(vo -> {
            LogId.put(vo.getLogId());
            // 数据脱敏
            String desensitizedRequestParam = DesensitizedOnJsonConvertor.desensitized(vo.getRequestParam());
            String desensitizedResponseParam = DesensitizedOnJsonConvertor.desensitized(vo.getResponseParam());
            vo.setRequestParam(desensitizedRequestParam);
            vo.setResponseParam(desensitizedResponseParam);
            log.info("{}", vo);
        });

        // 记录到数据库
        consumers.add(vo -> {
            LogId.put(vo.getLogId());
            SystemLogSaveRequest request = new SystemLogSaveRequest();
            request.setLogId(distributedSnowflake.nextIdStr());
            request.setRequestIp(vo.getRequestIp());
            if (ObjectUtils.isNotEmpty(vo.getLogType())) {
                request.setLogType(vo.getLogType().getCode());
            }
            request.setDescription(vo.getDescription());
            request.setClassPath(vo.getClassPath());
            request.setActionMethod(vo.getActionMethod());
            request.setHttpMethod(vo.getHttpMethod());
            request.setRequestParam(vo.getRequestParam());
            request.setResponseParam(vo.getResponseParam());
            request.setRequestUri(vo.getRequestUri());
            request.setStartTime(vo.getStartTime());
            request.setEndTime(vo.getFinishTime());
            request.setCostTime(vo.getConsumingTime());
            request.setError(vo.getError());
            request.setCreateUser(vo.getUserName());
            request.setCreateUserId(vo.getUserId());
            systemLogService.save(request);
        });
        return new SysLogEventListener(consumers);
    }


    @Bean
    @ConditionalOnProperty(name = {"log-tool.log.operate.enable"}, havingValue = "true")
    @Primary
    public OperateLogProcessor operateLogProcessor() {
        return new DefaultOperateLogProcessor();
    }
}
